package com.idroc.coelacanth.core.membership;

import org.jboss.netty.buffer.ChannelBuffer;

import com.idroc.coelacanth.core.message.AbstractMessage;

/**
 * 端点成员关闭消息
 * @author yuyoo (yuyoo4j@163.com)
 * @date Dec 5, 2009 4:26:30 PM
 */
public class MemberOpenToCloseMessage extends AbstractMessage {
	
	/**
	 * 节点
	 */
	private  Endpoint node = null;
	
	/**
	 * 心跳版本
	 */
	private  long pulseVersion = -1;
	
	/**
	 * 心跳时间
	 */
	private  long pulseTime = -1;
	
	/**
	 * 心跳有效期
	 */
	private  long lifecycle = -1;
	
	/**
	 * 心跳消息跳转次数
	 */
	private int hopToLive = 0;
	

	@Override
	public void decode(ChannelBuffer ib) {

		pulseVersion = ib.readLong();
		pulseTime = ib.readLong();
		lifecycle = ib.readLong();
		hopToLive = ib.readInt();
		 
		node = new Endpoint();
		node.setHost(decodeString(ib));
		node.setPort(ib.readInt());
	}

	@Override
	public void encode(ChannelBuffer ob) {

		ob.writeLong(pulseVersion);
		ob.writeLong(pulseTime);
		ob.writeLong(lifecycle);
		ob.writeInt(hopToLive);
		encodeString(ob, node.getHost());
		ob.writeInt(node.getPort());
	}

	Endpoint getNode() {
		return node;
	}

	void setNode(Endpoint node) {
		this.node = node;
	}

	long getPulseVersion() {
		return pulseVersion;
	}

	void setPulseVersion(long pulseVersion) {
		this.pulseVersion = pulseVersion;
	}

	long getPulseTime() {
		return pulseTime;
	}

	void setPulseTime(long pulseTime) {
		this.pulseTime = pulseTime;
	}

	long getLifecycle() {
		return lifecycle;
	}

	void setLifecycle(long lifecycle) {
		this.lifecycle = lifecycle;
	}

	int getHopToLive() {
		return hopToLive;
	}

	void setHopToLive(int hopToLive) {
		this.hopToLive = hopToLive;
	}

	@Override
	public String toString() {
		
		StringBuilder sb = new StringBuilder();
		sb.append("MemberOpenToCloseMessage[");
		sb.append("sn:").append(this.getSN()).append(",");
		sb.append("node:").append(node).append(",");
		sb.append("pulseVersion:").append(pulseVersion).append(",");
		sb.append("pulseTime:").append(pulseTime).append(",");
		sb.append("lifecycle:").append(lifecycle).append(",");
		sb.append("hopToLive:").append(hopToLive);
		sb.append("]");
		return sb.toString();
	}

}
